<?php

namespace App\Console\Commands\Tasks\Data;
use App\Repositories\DataSummaryRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;

class ComputeSummaryUser extends Command
{
    //命令名
    protected $signature = 'task:compute-summary-user {is_clear=0}';

    //命令描述
    protected $description = '计算用户汇总数据';

    // 业务处理
    public function handle()
    {
        $this->info("运行时间:" . date("Y-m-d H:i:s"));
        try {
            $isClear = $this->argument('is_clear');
            if (empty($isClear)) {
                // 增量更新
                $this->dailyDeal(Carbon::create()->subDay()->startOfDay());
            } else {
                // 全量更新
                $this->allDeal();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function allDeal() {
        $repo = app("repo_data_summary");
        $totalData = $repo->batchGetUserTotal();
        if (empty($totalData) || $totalData->isEmpty()) {
            $this->warn("没有全量数据！");
        } else {
            $userTotalData = $repo->batchGetUserTotal();

            $repo->insertUserTotal($userTotalData, true);

            $this->info("全量数据生成完毕！");
        }
    }

    public function dailyDeal(Carbon $date) {
        $repo = app("repo_data_summary");
        $userReceiveList = $repo->batchGetUserReceiveGiftDaily($date);

        $repo->insertUserTotal($userReceiveList, false);

        $this->info($date->toDateString() . "的增量数据添加完毕！");
    }

}